import {
    AuthUnionType,
    SIGNIN,
    SIGNIN_FAIL,
    SIGNIN_SUCCESS,
    SIGNUP,
    SIGNUP_FAIL,
    SIGNUP_SUCCESS
} from "../actions/auth.action"

export interface AuthState {
    signIn:{
        loaded: boolean
        success: boolean
        message: string
    },
    signUp:{
        loaded: boolean
        success: boolean
        message: string
    },
}

const intialState: AuthState = {
    signIn: {
      loaded: false,
      success: false,
      message: ""
    },
    signUp: {
        loaded: false,
        success: false,
        message: ""
      }
  }


  export default function authReducer(state = intialState, action:AuthUnionType) {
    switch (action.type) {
        case SIGNIN:
            
            return {
                ...state,
                signIn:{
                    loaded: false,
                    success: false,
                    message: ""
                }
            }
        case SIGNIN_SUCCESS:
            
            return {
                ...state,
                signIn:{

                    loaded: true,
                    success: true,
                    message: ""
                }
            }
            case SIGNIN_FAIL:
            
            return {
                ...state,
                signIn:{
                    loaded: true,
                    success: false,
                    message: action.message
                }
            }
            case SIGNUP:
            
            return {
                ...state,
                signUp:{
                    loaded: false,
                    success: false,
                    message: ""
                }
            }
        case SIGNUP_SUCCESS:
            
            return {
                ...state,
                signUp:{

                    loaded: true,
                    success: true,
                    message: ""
                }
            }
            case SIGNUP_FAIL:
            
            return {
                ...state,
                signUp:{
                    loaded: true,
                    success: false,
                    message: action.message
                }
            }
        default:
            return state    
        }
}